package com.itheima.demo;

import java.util.Scanner;

public class Test1calculator {
    public static void main(String[] args) {
        //目标：简易版计算器开发
        //1、键盘输入两个数字及其运算符
        Scanner sc = new Scanner(System.in);    //new个对象

        //获取第一个数字
        System.out.println("请输入第一个数字：");
        double num1 = sc.nextDouble();

        //获取运算符
        System.out.println("请输入运算符（+、-、*、/）：");
        String operator = sc.next();

        //获取第二个数字
        System.out.println("请输入第二个数字：");
        double num2 = sc.nextDouble();

        //2、把这个数据交给一个独立的计算方法，帮我计算结果并返回给我
        double result = getResult(num1, operator, num2);
        System.out.println("计算结果为：" + result);
    }

    public static double getResult(double num1, String operator, double num2) {
        double result = 0;
        switch (operator) {     //单值匹配
            case "+":
                result = num1 + num2;
                break;
            case "-":
                result = num1 - num2;
                break;
            case "*":
                result = num1 * num2;
                break;
            case "/":
                result = num1 / num2;
                break;
            default:
                System.out.println("输入的运算符有误！");
        }
        return result;
    }
}
